﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using CarFrameWork.Common;
using CarFrameWork.UI.Common;

namespace CarFrameWork.UI.ProductDesign.PartExamine.HouQiao
{
    public partial class usCtlQiaokeCanShu : BaseControl
      {
        private int iOperatorType = 0;//0:新增；1：更新


        public usCtlQiaokeCanShu()
        {
            InitializeComponent();
        }


        String[,] groupBox1Array = {{ "车轮(包括制动器等)的重力(N)", "WheelMass" } ,{ "汽车满载驱动桥静载荷", "QiaoLoads" }
                , { "车轮滚动半径", "WheelRadius" }, { "两钢板弹簧座中心间距离", "SpringDistance" },{ "后轮轮距", "WheelDistance" }};//标签与文本框的对应



        public Boolean checkXingHaoQiaoKe()
        {
            if (cmbQiaoKeXH.Text.CompareTo("") == 0)
            {
                MsgBox.Show("请选择桥壳型号！");
                cmbQiaoKeXH.Focus();
                return false;
            }
            return true;
        }

        private Boolean checkAllData()
        {
            if (!checkXingHaoQiaoKe() || !ComponentDeal.checkGroupBoxData(groupBox1, groupBox1Array)) return false;
            return true;
        }

        private void usCtlQiaokeCanShu_Load(object sender, EventArgs e)
        {
            BindQiaoKeXH();
            btnSave.Enabled = GetSaveButtonState().CompareTo("r") == 0 ? false : true;
        }

        private void cmbQiaoKeXH_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindQiaoKeCS();
        }

        private void BindQiaoKeXH()
        {

            DataTable dtOld = SqlWork.select("select distinct 桥壳型号 from 后桥选型参数", "后桥选型参数");

            cmbQiaoKeXH.ValueMember = "桥壳型号";
            cmbQiaoKeXH.DisplayMember = "桥壳型号";
            cmbQiaoKeXH.DataSource = ComponentDeal.cmbNewBlank(dtOld, "桥壳型号");
            cmbQiaoKeXH.DropDownStyle = ComboBoxStyle.DropDownList;
        }

      

        private void BindQiaoKeCS()
        {
            string strQiaoKeXH = cmbQiaoKeXH.Text;
            
            string strSql = "select 满载驱动桥静载荷,车轮重力,后轮轮距,钢板弹簧座中心间距,车轮半径  from  桥壳公共系数表   where 桥壳型号='" + strQiaoKeXH + "'";

            DataTable dt = SqlWork.select(strSql, "桥壳公共系数表");
            
            if (dt.Rows.Count == 0)
            {
                iOperatorType = 0;
                this.QiaoLoads.Text = "";
                this.WheelMass.Text = "";
                this.WheelDistance.Text = "";
                this.SpringDistance.Text = "";
                this.WheelRadius.Text = "";
            }
            else
            {
                iOperatorType = 1;
                this.QiaoLoads.Text = dt.Rows[0][0].ToString();
                this.WheelMass.Text = dt.Rows[0][1].ToString();
                this.WheelDistance.Text = dt.Rows[0][2].ToString();
                this.SpringDistance.Text = dt.Rows[0][3].ToString();
                this.WheelRadius.Text = dt.Rows[0][4].ToString();             
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!checkAllData()) return;

            string strInsertSql = "insert into 桥壳公共系数表(桥壳型号,满载驱动桥静载荷,车轮重力,后轮轮距,钢板弹簧座中心间距,车轮半径) values('"
                        + cmbQiaoKeXH.Text + "','" +  QiaoLoads.Text + "','" + WheelMass.Text + "','" + WheelDistance.Text + "','"
                        + SpringDistance.Text + "','" + WheelRadius.Text + "')";
            string strUpdateSql = "update 桥壳公共系数表 set 满载驱动桥静载荷=" + QiaoLoads.Text + ",车轮重力=" + WheelMass.Text
                        + ",后轮轮距=" + WheelDistance.Text + ",钢板弹簧座中心间距=" + SpringDistance.Text + ",车轮半径=" + WheelRadius.Text
                        + " where 桥壳型号='" + cmbQiaoKeXH.Text + "'";
                
            ComponentDeal.insertOrUpdateDataBase(iOperatorType, strInsertSql, strUpdateSql);

            BindQiaoKeCS();
        }

   
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.cmbQiaoKeXH.Text = "";
            this.QiaoLoads.Text = "";
            this.WheelMass.Text = "";
            this.WheelDistance.Text = "";
            this.SpringDistance.Text = "";
            this.WheelRadius.Text = "";
        }
    }
}
